/**
 * Created by wangshaohua on 2018/1/23.
 */
moduleLearningMap.controller("ConfigDoorForCourse", function ($$CambioConfig, $scope, $log, $state, LoadingService, DialogService, EnumService, ConstantService, DoorResource, GeneralResource) {
    initPage($scope);
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        $scope.queryDoorKey = null;
    });

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.queryDoor = function () {
        $scope.queryPara = {
            className: Models.Event.className,
            query: {
                code: {
                    field: Models.Event.query.code,
                    matching: Matching.LIKE,
                    value: $scope.queryDoorKey
                },
                name: {
                    field: Models.Event.query.type.name,
                    value: 'STUDY_MAP',
                    enumClass: Models.Event.query.type.enumName
                }
            }
        };
        //LoadingService.show();
        GeneralResource.queryPage({pageNo: 0, pageSize: 50}, $scope.queryPara, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.doorResults = queryResult.data.datas;
            //LoadingService.hide();
        }, function () {
            //LoadingService.hide();
        });
    };

    $scope.queryCourse = function () {
        //LoadingService.show();

        $scope.queryPara = {
            className: Models.LearningMapCourse.className,
            query: {
                learningMapId: {
                    field: Models.LearningMapCourse.query.learningMapId,
                    value: $scope.learningMapId
                },
                key: {
                    field: Models.LearningMapCourse.query.code,
                    matching: Matching.LIKE,
                    value: $scope.queryCourseKey
                }
            }
        };

        GeneralResource.queryPage({pageNo: 0, pageSize: 10}, $scope.queryPara, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.courseResults = queryResult.data.datas;
            //LoadingService.hide();
        });
        //
        //BadgeResource.findByKey({}, {key: $scope.badgeKey}, function (badgeResult) {
        //    $log.debug("勋章匹配结果", badgeResult);
        //    $scope.badgeResult = badgeResult;
        //    //LoadingService.hide();
        //});
    };

    //setCurrentCourse
    $scope.setCurrentCourse = function (course) {
        $scope.currentConfig.course = course;
        $scope.currentConfig.courseId = course.id;
    };

    $scope.setCurrentDoor = function (door) {
        $scope.currentConfig.event = door;
        $scope.currentConfig.eventId = door.id;
    };

    $scope.saveConfigDoorForCourse = function () {

        //currentConfig
        Alert.confirm(function () {
            //LoadingService.show();
            DoorResource.configForCourse({}, $scope.currentConfig, function () {
                //LoadingService.hide();
                Alert.success();
                DialogService.close();
            }, function () {
                //LoadingService.hide();
            });
        });
    };
});